<?php


/**
 * Created on 7 mai 2011
 * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
require_once("/var/www/chemistry-repository/chemistry-repository/trunk/main/webapp/Model/PajaxRemote.class.php");
 
 
class Atom extends PajaxRemote{
    /*
     * Attributes
     */

    private $_atomID; //auto increment in the database
    private $_symbol;
    private $_isAromatic;
    private $_isVisible;
    private $_formalCharge;
    private $_atomicMass;
    private $_signature;
    private $_moleculeID;
    private $_innerId;


    /*
     * Constructor
     */
     
    public function Atom(){
    	$this->_atomID = -1;
        $this->_symbol = null;
        $this->_isAromatic = null;
        $this->_isVisible = null;
        $this->_formalCharge = null;
        $this->_atomicMass = null;
        $this->_signature = null;
        $this->_moleculeID = null;
        $this->_innerId = null;
    	
    }
     
    public function __Atom($innerId, $symbol, $isAromatic, $isVisible, $formalCharge, $atomicMass, $signature, $moleculeId="", $atomID=-1) {
        $this->_atomID = $atomID;
        $this->_symbol = $symbol;
        $this->_isAromatic = $isAromatic;
        $this->_isVisible = $isVisible;
        $this->_formalCharge = $formalCharge;
        $this->_atomicMass = $atomicMass;
        $this->_signature = $signature;
        $this->_moleculeID = $moleculeId;
        $this->_innerId = $innerId;
    }

    /*
     * Accessors
     */

    public function getAtomId() {
        return $this->_atomID;
    }

    public function getSymbol() {
        return $this->_symbol;
    }

    public function getIsAromatic() {
        return $this->_isAromatic;
    }

    public function getIsVisible() {
        return $this->_isVisible;
    }

    public function getFormalCharge() {
        return $this->_formalCharge;
    }

    public function getAtomicMass() {
        return $this->_atomicMass;
    }

    public function getSignature() {
        return $this->_signature;
    }

    public function getMoleculeId() {
        return $this->_moleculeId;
    }

    /*
     * Modifiers
     */

    public function setAtomId($param) {
        $this->_atomID = $param;
    }

    public function setSymbol($param) {
        $this->_symbol = $param;
    }

    public function setIsAromatic($param) {
        $this->_isAromatic = $param;
    }

    public function setIsVisible($param) {
        $this->_isVisible = $param;
    }

    public function setFormalCharge($param) {
        $this->_formalCharge = $param;
    }

    public function setAtomicMass($param) {
        $this->_atomicMass = $param;
    }

    public function setSignature($param) {
        $this->_signature = $param;
    }

    public function setMoleculeId($param) {
        $this->_moleculeId = $param;
    }


	public static function caca(){

		$file="Test";
		if(class_exists($file)){
			echo "In atom the class $file exists\n  </br>";
		}else{
			echo "In atom the class $file does not exists\n";
		}
				
	}

    /*
     * CRUD
     */

    /**
     * Put an object in the database
     */
    public function create() {
        $connection=Database::connect();
        if ($this->_atomID != -1)
            die("Unable to create, still existing !");
        else {
            $query = "INSERT INTO `ATOM` values (NULL,'" .
                    $this->_symbol . "','" .
                    $this->_isAromatic . "','" .
                    $this->_isVisible . "','" .
                    $this->_formalCharge . "','" .
                    $this->_atomicMass . "','" .
                    $this->_signature . "','" .
                    $this->_moleculeID . "')";
            mysql_query($query) or die(mysql_error());
            $this->_idSujet = mysql_insert_id();
            Database::disconnect($connection);
        }
//        return $this->_idSujet;
        Database::disconnect($connection);
    }
    
    public static function createAndReturn($arrayOfAtoms,$moleculeID) {
		$mapOfAtomIds;
    	$connection=Database::connect();
//    	var_dump("</br></br>");
    	foreach($arrayOfAtoms as $atom){
   			$atom->_moleculeID=$moleculeID;
    		if ($atom->_atomID != -1)
            	die("Unable to create, still existing !");
     		else {
//     		echo($atom->_symbol." ; ".$atom->_isAromatic." ; ".$atom->_isVisible." ; ".$atom->_formalCharge." ; ".$atom->_atomicMass." ; ".$atom->_signature." ; ".$atom->_moleculeID);
            $query = "INSERT INTO `ATOM` values (NULL,'" .
                    $atom->_symbol . "','" .
                    $atom->_isAromatic . "','" .
                    $atom->_isVisible . "','" .
                    $atom->_formalCharge . "','" .
                    $atom->_atomicMass . "','" .
                    $atom->_signature . "','" .
                    $atom->_moleculeID . "')";
            mysql_query($query) or die(mysql_error());
            $atom->_atomID= mysql_insert_id();
            $mapOfAtomIds[$atom->_innerId]=$atom->_atomID;            
    	    }
    	}
    	Database::disconnect($connection);
    	return $mapOfAtomIds;
    }

    /**
     * Read an object from the database
     */
    public static function read($key) {
        $connection=Database::connect();
        $query = "SELECT * FROM ATOM WHERE atom_id = '" . $key . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        $Atom=new Atom();
        $Atom->__Atom($atom_id, $symbol, $is_aromatic, $is_visible,
                $formal_charge, $atomic_mass, $signature, $molecule_id);
		Database::disconnet();
        return $Atom;
    }

    /**
     * Update an object from the database
     */
    public function update() {
        $connection=Database::connect();
        $query = "UPDATE `ATOM` SET symbol='" .
                $this->_symbol . "', is_aromatic='" .
                $this->_isAromatic . "', is_visible='" .
                $this->_isVisible . "', formal_charge='" .
                $this->_formalCharge . "', atomic_mass='" .
                $this->_atomicMass . "', signature='" .
                $this->_signature . "', molecule_id='" .
                $this->_moleculeID . "', WHERE atom_id='" .
                $this->_atomID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }

    /**
     * Delete an object from the database
     */
    public function delete() {
        $connection=Database::connect();
        $query = "DELETE FROM `ATOM` WHERE atom_id='" . $this->_atomID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }
    
    
    
    public function getAllAtomsFromMolecule($moleculeId){
    	$connection=Database::connect();
    	$arrayOfAtoms=array();
    	$query="SELECT * FROM ATOM where molecule_id='".$moleculeId."'";
    	$ans=mysql_query($query);

    	while ($row = mysql_fetch_object($ans)) {
//    		echo $row->atom_id."</br>";
			$atom=new Atom();
    		$atom->__Atom(null,$row->symbol ,$row->is_aromatic ,$row->is_visible ,$row->formal_charge ,$row->atomic_mass ,$row->signature ,$row->molecule_id,$row->atom_id);
    		$secondanswer=mysql_query("select * from COORDINATE_SET_2D where atom_id='".$row->atom_id."'");
    		$secondrow=mysql_fetch_object($secondanswer);
    		$coordinate=new Coordinates();
    		$coordinate->__Coordinates($secondrow->atom_id,$secondrow->x_coordinate ,$secondrow->y_coordinate,$secondrow->coordinate_set_id);
//    		echo $coordinate->getxCoordinate();
    		$small=array($atom,$coordinate);
    		$arrayOfAtoms[$atom->_atomID ]=$small;
//    		array_push($arrayOfAtoms,$small);
//    		echo $atom->getAtomId()." ---->  ".$coordinate->getxCoordinate()."</br>";
		}
    	Database::disconnect($connection);
    	
    	return $arrayOfAtoms;
    }
    
    
    
    
    

}
?>
